<?php
declare (strict_types = 1);

namespace app\command;

use app\common\model\QqGroup;
use fast\Http;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class Cron extends Command
{
    protected function configure()
    {
        $this->setName('cron')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('trade', null, Option::VALUE_NONE, 'open trade')
            ->addOption('notify', null, Option::VALUE_NONE, 'open notify')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $name = trim($input->getArgument('name'));
        $name = $name ?: 'thinkphp';

        switch ($name) {
            case 'test':
                $list = send();
                if($list === false){
                    //错误
                }
                $result = json_decode($list,true);
                if(empty($result['mems'])){
                    //错误
                }
                foreach ($result['mems'] as $item)
                {
//                    dump($item);die;
                    //写入QQ群
                    QqGroup::create([
                        'qq'        => $item['uin'],
                        'join_time' => $item['join_time'],
                        'nickname'  => $item['nick']
                    ]);
                }
                dump($result);die;
                break;
        }
    }

}

function send(){
    $cookie = 'pgv_pvid=2346397384; RK=uL8MS1Cx+a; tvfe_boss_uuid=2e59be385ee1cb51; o_cookie=2171734506; qq_domain_video_guid_verify=88b40f6eaccbd6ec; eas_sid=d1R6M9U9J057B7s0G8p2C906a2; LW_uid=11T790V3t2c4Q9M618y736C8S0; ptcz=7843229f09d464a7239a197173b08b7ccc84933425726cab177ca72015e18498; LW_sid=t1E7n00676e175H7c106L3P2L5; ptui_loginuin=2171734506; _clck=3939698923|1|fm4|0; uin=o2171734506; _qpsvr_localtk=0.2210633223081646; mail5k=8f1c148d; tgw_l7_route=f1dd34063017ea8b03d2c90177ee5ce6; skey=@RYXgs4d3Q; p_uin=o2171734506; pt4_token=53aROrHW1JPYo8O-jqKT-sEvuEJopkstJ6xlyOG0uws_; p_skey=IZUg16v*FDyA56ltyY6znSXPib4flewgFHBkFgv8rGM_; traceid=cad3738b43'; // 替换为实际的 Cookie，用于身份验证
    $curl = curl_init();
    $url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members";
    $postData = http_build_query([
        'gc' => '906015038',
        'st' => '0',
        'end' => '10',
        'sort' => '0',
        'bkn' => '429444382', // 这里的 bkn 需要根据实际情况获取，可以通过查看浏览器开发工具获取
    ]);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($curl, CURLOPT_USERAGENT, "EOLINKER");
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded',
        'Cookie: ' . $cookie,
    ]);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    if ($response === false) {
        return false;
    }

    curl_close($curl);
    return $response;
}